package initlize

import (
	"SE/global"
	"SE/model"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func InitMysql() {
	mysqlInfo := global.Settings.MysqlInfo
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		mysqlInfo.Name, mysqlInfo.Password, mysqlInfo.Host,
		mysqlInfo.Port, mysqlInfo.DBName)
	db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	// 自动迁移模式
	err := db.AutoMigrate(&model.User{})
	if err != nil {
		return
	}

	err = db.AutoMigrate(&model.Car{})
	if err != nil {
		return
	}

	err = db.AutoMigrate(&model.CarP{})

	if err != nil {
		return
	}
	err = db.AutoMigrate(&model.P{})
	if err != nil {
		return
	}

	err = db.AutoMigrate(&model.Manager{})
	if err != nil {
		return
	}
	global.DB = db
}
